//
//  MyShieldViewController.m
//  Findif
//
//  Created by Mac on 2019/3/25.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "MyShieldViewController.h"
#import "GroupMemberTableViewCell.h"
#import "MemberModel.h"

@interface MyShieldViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <MemberModel *> * buyList;
@property (nonatomic, weak)UIImageView * spaceImageView;
@property (nonatomic, weak)UILabel * spaceLabel;

@end

@implementation MyShieldViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    [self loadData];
}

- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getShieldUserListWithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.buyList = [NSArray yy_modelArrayWithClass:[MemberModel class] json:responseData[@"data"]];
        }
    }];
    
//    [self.tableView.mj_header endRefreshing];
}


- (void)setBuyList:(NSArray<MemberModel *> *)buyList{
    
    _buyList = buyList;
    
    [self.tableView reloadData];
}

- (void)setupUI{
    
    self.title = @"我的屏蔽";
    self.view.backgroundColor = ColorWithRGB(0xF0F0F0);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF0F0F0);
    tableView.rowHeight = 60 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[GroupMemberTableViewCell class] forCellReuseIdentifier:@"MyShieldTableViewCell"];
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//
//        [self loadData];
//    }];
    
    [self.view addSubview:self.tableView];
    
    
    UIImageView * spaceImageView = [UIImageView new];
    
    spaceImageView.image = [UIImage imageNamed:@"状态为空"];
    spaceImageView.hidden = YES;
    
    self.spaceImageView = spaceImageView;
    
    [self.tableView addSubview:self.spaceImageView];
    
    
    UILabel * spaceLabel = [UILabel new];
    
    spaceLabel.text = @"您目前没有屏蔽的对象";
    spaceLabel.textColor = [UIColor lightGrayColor];
    spaceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    spaceLabel.textAlignment = NSTextAlignmentCenter;
    spaceLabel.hidden = YES;
    
    self.spaceLabel = spaceLabel;
    
    [self.tableView addSubview:self.spaceLabel];
    
}


- (void)viewDidLayoutSubviews{
    
    [self.spaceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.tableView);
        make.centerY.mas_equalTo(self.tableView).offset(-30 * SCREENH6);
        make.width.mas_equalTo(169 * SCREENH6);
        make.height.mas_equalTo(136 * SCREENH6);
    }];
    
    [self.spaceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.spaceImageView);
        make.top.mas_equalTo(self.spaceImageView.mas_bottom).offset(10 * SCREENH6);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.buyList.count == 0) {
        
        self.spaceImageView.hidden = NO;
        self.spaceLabel.hidden = NO;
    }else{
        
        self.spaceImageView.hidden = YES;
        self.spaceLabel.hidden = YES;
    }
    return self.buyList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GroupMemberTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyShieldTableViewCell" forIndexPath:indexPath];
    
    cell.memberModel = self.buyList[indexPath.row];
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                   message:@"确定解除对他的信息屏蔽吗？解除之后，你将能看到他发布的信息。"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              
                                                              MemberModel * model = self.buyList[indexPath.row];
                                                              NSString * ID = [NSString stringWithFormat:@"%@",model.id];
                                                              
                                                              [self updateState:ID];
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {
                                                             
                                                         }];
    
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}


- (void)updateState:(NSString *)ID{
    
    [[ZRNetworkTool sharedTool] deleteShieldUserWithUser_id:ID WithBlock:^(BOOL success, id responseData) {

        if (success) {
            
            self.buyList = nil;
            [self loadData];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}


@end
